/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.core.wicket.application;

import com.eelpo.cmmp.core.wicket.application.mount.IWebApplicationMount;
import com.eelpo.cmmp.core.wicket.application.setting.Settings;
import com.eelpo.cmmp.core.wicket.session.ISessionGenerate;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;

/**
 * <pre>
 * 
 * 系统Wicket配置管理器,定义程序入口及相关策略
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
@Controller
public class CMMPWebApplication extends WebApplication implements ApplicationContextAware {

	/**
	 * 设置工具类
	 */
	@Autowired(required = false)
	private Settings settings;

	/**
	 * Session生成器
	 */
	private ISessionGenerate sessionGenerate;

	/**
	 * Wicket应用程序初始化
	 */
	@Override
	protected void init() {

		settings.settings(this);

		sessionGenerate = settings.getSessionGenerate();
	}

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		String[] mounts = context.getBeanNamesForType(IWebApplicationMount.class);
		for (String mount : mounts) {
			((IWebApplicationMount) context.getBean(mount)).mount(this);
		}
	}

	@Override
	public Session newSession(Request request, Response response) {
		return getSessionGenerate().newSession(request, response);
	}

	@Override
	public Class<? extends Page> getHomePage() {
		return settings.getHomePage();
	}

	public ISessionGenerate getSessionGenerate() {
		return sessionGenerate;
	}

	public void setSettings(Settings settings) {
		this.settings = settings;
	}

}
